package com.nami.logic.rocketmq.consumer;

import com.github.yitter.idgen.YitIdHelper;
import com.nami.basic.bo.servicetrans.up.UpMsgWrap;
import com.nami.basic.enums.DownMsgErrorCode;
import com.nami.basic.enums.protocol.DownCMDEnum;
import com.nami.basic.packet.message.Message;
import com.nami.gate.api.request.message.DownMsgRequest;
import com.nami.logic.bo.send.DownModeConfig;
import com.nami.logic.service.DownMsgSender;
import com.nami.logic.service.PacketFactory;
import com.nami.store.api.bo.UserWrap;
import com.nami.store.api.request.cache.SeqRequest;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.enums.user.UserStatusEnum;
import org.jetbrains.annotations.NotNull;

import static com.nami.basic.constants.CommonConst.COLON;

public abstract class AbstractMessage {

    public static final String DOWN_MSG_TRANS_MODE = "mq";


    protected void addSystemInfoToThisMsg(Message msg) {
        msg.setTime(System.currentTimeMillis());
        msg.setId(YitIdHelper.nextId() + "");
    }

    /**
     * 校验msg
     *
     * @param wrapper
     * @param msg
     * @return
     */
    protected boolean checkNullParams(UpMsgWrap wrapper, Message msg) {
        if (null == wrapper.getDeviceType()
            || StringUtils.isBlank(msg.getPayload())
            || StringUtils.isBlank(msg.getSenderId())
            || StringUtils.isBlank(msg.getReceiverId())
            || StringUtils.isBlank(msg.getClientMsgId())) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    protected void sendErrorResp(
        DownMsgSender sender,
        Message message, String identityNo, UpMsgWrap upMsgWrap,
        DownMsgErrorCode errorCode, PacketFactory packetFactory) throws Exception {
        DownMsgRequest request = new DownMsgRequest();
        request.setMessageId(message.getId());
        request.setIdentityNo(identityNo);
        request.setPacket(packetFactory.newDownmsgErrorRespPacket(DownCMDEnum.C2C.getCode(), errorCode, message));

        sender.flushOne(request, newModeConfig(upMsgWrap));
//        ForwardPacketUtil.flushPacket(request, upMsgWrap.getDubboIp(), upMsgWrap.getDubboPort());
    }

    protected void sendErrorResp(
        DownMsgSender sender, Integer downCmd,
        Message message, String identityNo, UpMsgWrap upMsgWrap,
        DownMsgErrorCode errorCode, PacketFactory packetFactory) throws Exception {
        DownMsgRequest request = new DownMsgRequest();
        request.setMessageId(message.getId());
        request.setIdentityNo(identityNo);
        request.setPacket(packetFactory.newDownmsgErrorRespPacket(downCmd, errorCode, message));

        sender.flushOne(request, newModeConfig(upMsgWrap));
//        ForwardPacketUtil.flushPacket(request, upMsgWrap.getDubboIp(), upMsgWrap.getDubboPort());
    }


    protected DownModeConfig newModeConfig(UpMsgWrap upMsgWrap) {
        DownModeConfig config = new DownModeConfig();
        config.setMode(DOWN_MSG_TRANS_MODE);
        config.setMqTags(upMsgWrap.getMqTags());
        config.setDubboIp(upMsgWrap.getDubboIp());
        config.setDubboPort(upMsgWrap.getDubboPort());
        return config;
    }

    protected boolean checkUserWasBan(UpMsgWrap c2CMsg, UserWrap user, String identityNo) {
        if (UserStatusEnum.BAN.getCode() == user.getStatus()) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }


    /**
     * 生成消息指纹
     *
     * @param userNo
     * @param clientMsgId
     * @return
     */
    @NotNull
    protected static String genUserMsgFingerChar(String userNo, String clientMsgId) {
        return userNo + COLON + clientMsgId;
    }

    @NotNull
    protected static SeqRequest buildSequenceDTO(String appId, String cid) {
        SeqRequest sequenceDTO = new SeqRequest();
        sequenceDTO.setAppId(appId);
        sequenceDTO.setCid(cid);
        return sequenceDTO;
    }


}
